<?php

class Application_Model_DbTable_User extends Zend_Db_Table_Abstract
{

    protected $_name = 'ojuser';
    
    public function addUser(Application_Model_User $user){
        $data = array(
            'ojuName' => $user->getName(),
            'ojuPassword' => $user->getPassword(),
            'ojuEmail' => $user->getEmail(),
            'ojuGender' => $user->getGender(),
            'ojuQQ' => $user->getQq(),
            'ojuMSN' => $user->getMsn(),
            'ojuImgDefined' => $user->getImgDefined(),
            'ojuImgUrl' => $user->getImgUrl(),
            'ojuRegisterTime' => $user->getRegisterTime(),
            'ojuLastTime' => $user->getLastTime(),
            'ojuAdmin' => $user->getAdmin(),
            'ojuMemo' => $user->getMemo()
        );
        $id = $this->insert($data);
        $user->setId($id);
        return $this;
    }
    
    public function updateUser(Application_Model_User $user){
        $data = array(
            'ojuName' => $user->getName(),
            'ojuPassword' => $user->getPassword(),
            'ojuEmail' => $user->getEmail(),
            'ojuGender' => $user->getGender(),
            'ojuQQ' => $user->getQq(),
            'ojuMSN' => $user->getMsn(),
            'ojuImgDefined' => $user->getImgDefined(),
            'ojuImgUrl' => $user->getImgUrl(),
            'ojuRegisterTime' => $user->getRegisterTime(),
            'ojuLastTime' => $user->getLastTime(),
            'ojuAdmin' => $user->getAdmin(),
            'ojuMemo' => $user->getMemo()
        );
        $this->update($data, 'ojuId = '.$user->getId());
        return $this;
    }
    
    public function deleteUser($id){
        $where = " ojuId = ".$id;
        $this->delete($where);
        return $this;
    }
    
    public function getUserByName($name,$pwd){
        $select = $this->select();
        $select->from('ojuser', '*');
        $select->where(' ojuName = ? ',$name);
        $select->where(' ojuPassword = ? ', $pwd);
        $select->order('ojuId');
        $rowset = $this->fetchAll($select);
        if (!$rowset || 0==count($rowset))
            return null;
        $row = $rowset->getRow(0);
        $user = new Application_Model_User();
        $user->setId($row->ojuId)
                ->setName($row->ojuName)
                ->setPassword($row->ojuPassword) 
                ->setEmail($row->ojuEmail)
                ->setGender($row->ojuGender)
                ->setQq($row->ojuQQ)
                ->setMsn($row->ojuMSN)
                ->setImgDefined($row->ojuImgDefined)
                ->setImgUrl($row->ojuImgUrl)
                ->setRegisterTime($row->ojuRegisterTime)
                ->setLastTime($row->ojuLastTime)
                ->setAdmin($row->ojuAdmin)
                ->setMemo($row->ojuMemo);
        return $user;  
    }
    
    public function getUserById($id){
        $row = $this->fetchRow('ojuId = '.$id);
        if(!$row)
            return null;
        $user = new Application_Model_User();
        $user->setId($id);
        $user->setName($row->ojuName);
        $user->setPassword($row->ojuPassword) ;
        $user->setEmail($row->ojuEmail);
        $user->setGender($row->ojuGender);
        $user->setQq($row->ojuQQ);
        $user->setMsn($row->ojuMSN);
        $user->setImgDefined($row->ojuImgDefined);
        $user->setImgUrl($row->ojuImgUrl);
        $user->setRegisterTime($row->ojuRegisterTime);
        $user->setLastTime($row->ojuLastTime);
        $user->setAdmin($row->ojuAdmin);
        $user->setMemo($row->ojuMemo);
        return $user;
    }
    
   public function getUserByWhere($where) {
        $rowset = $this->fetchAll($where);
        if(!$rowset || 0 == count($rowset))
            return null;
        $row = $rowset->getRow(0);
        $user = new Application_Model_User();
        $user->setId($row->ojuId)
            ->setName($row->ojuName)
            ->setPassword($row->ojuPassword) 
            ->setEmail($row->ojuEmail)
            ->setGender($row->ojuGender)
            ->setQq($row->ojuQQ)
            ->setMsn($row->ojuMSN)
            ->setImgDefined($row->ojuImgDefined)
            ->setImgUrl($row->ojuImgUrl)
            ->setRegisterTime($row->ojuRegisterTime)
            ->setLastTime($row->ojuLastTime)
            ->setAdmin($row->ojuAdmin)
            ->setMemo($row->ojuMemo);
        return $user;
       
   }
   
    public function getUsers(){
        $rowset = $this->fetchAll();
        $users = array();
        foreach ($rowset as $row) {
            $user = new Application_Model_User();
            $user->setId($row->ojuId)
                    ->setName($row->ojuName)
                    ->setPassword($row->ojuPassword) 
                    ->setEmail($row->ojuEmail)
                    ->setGender($row->ojuGender)
                    ->setQq($row->ojuQQ)
                    ->setMsn($row->ojuMSN)
                    ->setImgDefined($row->ojuImgDefined)
                    ->setImgUrl($row->ojuImgUrl)
                    ->setRegisterTime($row->ojuRegisterTime)
                    ->setLastTime($row->ojuLastTime)
                    ->setAdmin($row->ojuAdmin)
                    ->setMemo($row->ojuMemo);
            $users[] = $user;
        }
        return $users;
    }
    
    public function getUsersByRange($start,$offset){
        $rowset = $this->fetchAll(null,'ojuId',$offset,$start);
        $users = array();
        foreach ($rowset as $row) {
            $user = new Application_Model_User();
            $user->setId($row->ojuId)
                    ->setName($row->ojuName)
                    ->setPassword($row->ojuPassword) 
                    ->setEmail($row->ojuEmail)
                    ->setGender($row->ojuGender)
                    ->setQq($row->ojuQQ)
                    ->setMsn($row->ojuMSN)
                    ->setImgDefined($row->ojuImgDefined)
                    ->setImgUrl($row->ojuImgUrl)
                    ->setRegisterTime($row->ojuRegisterTime)
                    ->setLastTime($row->ojuLastTime)
                    ->setAdmin($row->ojuAdmin)
                    ->setMemo($row->ojuMemo);
            $users[] = $user;
        }
        return $users;
    }
    
    //已注册用户数量
    public function countUsers() {
        $select = $this->select();
        $select->from($this->_name,'COUNT(ojuId) AS num');
//        echo $select->__toString();
        return $this->fetchRow($select)->num;
    }
}

